<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>test</title>
</head>
<body>
  <script>
    // currying 原理
    // 内部返回一个函数
    // 收集参数
    // 判断 fn.length 原函数的形参 与 args.length 的长度
    // args.length >= fn.length 执行返回
    // args.length < fn.length 递归执行 next
    function currying(fn) {
      return function next() {
        let args = Array.prototype.slice.call(arguments)
        if(args.length >= fn.length) {
          return fn.call(this, ...args)
        }
        return function(...arg) {
          return next(...args, ...arg)
        }
      }
    }
    // 测试
    function addNum(a, b, c) {
      return a + b + c;
    }
    const addCurry = currying(addNum);
    console.log( addCurry(1)(2)(3) );  // 6
    console.log( addCurry(1) );     // ƒ (...arg) { return next(...args, ...arg) }
    console.log( addCurry(1)(2) );  // ƒ (...arg) { return next(...args, ...arg) }
    console.log( addCurry(1)(2, 3) ); // 6
    console.log( addCurry(1, 2)(3) ); // 6
    console.log( addCurry(1, 2, 3) ); // 6
 </script>
</body>
</html>